<?php

include_once ('bn_path.php');

class gridbtn
{
    public $caption = 'check all,uncheck all';
    public $enable = false;
    public $cssclass = '';
}

class checkbox
{
    public $text = '';
    public $value = '';
    public $checked = false;


    public function __construct($text = '', $value = '', $checked = false)
    {
        $this->text = $text;
        $this->value = $value;
        $this->checked = $checked;
    }
}



class bn_CheckBoxGroup
{
    public $name = 'bn_CheckBoxGroup1';
    public $text = '';
    public $value = '';
    public $checked = '';
    public $items;
    public $check;
    
    protected static $instance;
    
    function __construct()
    {
        $this->items = new ArrayObject();
        $this->check=new gridbtn;
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function create_list($text, $value, $checked)
    {
        if ($checked == $text || $checked == $value)
            return "\n<input type='checkbox' name='" . $this->name . "_check[]' value='$value' checked />$text<br>";
        else
            return "\n<input type='checkbox' name='" . $this->name . "_check[]' value='$value'/>$text<br>";
    }

    private function create_items()
    {
        $out = '';
        foreach ($this->items as $item)
        {
            if (is_object($item))
                $out .= self::create_list($item->text, $item->value, $item->checked);
        }
        return $out;
    }


    public function show($param = array())
    {
        if ($param != null || $this->items != null)
        {
            $row = $param[0];
            if (is_object($row))
                $row = (array )$row;
            if (count($row) > 1)
            {
                $key = array_keys($row);
                if (empty($this->value))
                    $this->value = $key[0];
                if (empty($this->text))
                    $this->text = $key[1];
                if (empty($this->checked) && isset($key[2]))
                    $this->checked = $key[2];
            }
            $checklist = "\n<table name='$this->name' id='$this->name'>";
            if($this->check->enable)
            {
            $name=explode(',',$this->check->caption);
            if($this->check->cssclass!='')
            $css=" class='".$this->check->cssclass."'";
             $checklist .="\n<thead>
             \n<tr>
             \n<td>
             <input type='submit' name='check_all' id='check_all' onclick='return false;' value='{$name[0]}'$css/>
             <input type='submit' name='uncheck_all' id='uncheck_all' onclick='return false;' value='{$name[1]}'$css/>
             \n</td>
             \n</tr>
             \n</thead>";   
            }
            foreach ($param as $row)
            {
                if (is_object($row))
                    $row = (array )$row;
                $cnt = count($row);
                $checklist .= "\n<tr>\n<td>";
                if ($cnt == 1)
                    $checklist .= "\n\t<input type='checkbox' name='chbox[]' value='$row'/>" . $row;
                elseif ($cnt == 2)
                    $checklist .= self::create_list($row[$this->text], $row[$this->value],$this->checked);
                else
                    $checklist .= self::create_list($row[$this->text], $row[$this->value], $row[$this->checked]);

                $checklist .= "\n</td>\n</tr>";
            }
            $checklist .= self::create_items();
            $checklist .= "\n</table>";
            echo $checklist;
        }
    }

    public function command_script($recivepage = '', $div = '#show')
    {
        if ($recivepage == '')
            $recivepage = 'window.location.href';
        else
            $recivepage = "'$recivepage'";
            $out='';
            
        if($this->check->enable)
        {
            $out.="$('#check_all').live('click',function(){
                $('#$this->name input[type=checkbox]').each(function(){
                    $(this).attr('checked','true');
                });
            });
            $('#uncheck_all').live('click',function(){
                $('#$this->name input[type=checkbox]').each(function(){
                    $(this).removeAttr('checked');
                });
            });";
        }
        
        $out.= "
        $('#$this->name input[type=checkbox]').live('click',function(){
            var ch='';
        if($(this).is(':checked'))
        ch='true';
        else
        ch='false';
        $.post($recivepage,{cmd:'$this->name',id:$(this).val(),state:ch},function(data){
            $('$div').html(data);
        });
        });";
        
        return $out;
    }

    function __destruct()
    {
        unset($this->items);
    }
}

?>